//
//  ViewController.m
//  UI_24_homework_加密
//
//  Created by li cheng on 16/8/16.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "NSString+LCMD5.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *encipherTextField;
@property (weak, nonatomic) IBOutlet UITextField *decrypTextField;
@property (strong, nonatomic) AFHTTPSessionManager *manger;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _manger = [AFHTTPSessionManager manager];
}

- (void)urlString:(NSString *)urlString headString:(NSString *)string paramenters:(NSString *)paramenters{
    
    // 设置请求参数的字符串格式,自定义
    [_manger.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return paramenters;
    }];
    // 正常POST请求
    [_manger POST:urlString parameters:paramenters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        if ([urlString  isEqual: @"http://tool.zzblo.com/api/md5/encrypt"]) {
            _decrypTextField.text = [NSString stringWithFormat:@"%@", responseObject[@"secret32"]];
            _encipherTextField.text = nil;
        } else {
            _encipherTextField.text = [responseObject objectForKey:@"text"];
            _decrypTextField.text = nil;
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (IBAction)encipherButton:(id)sender {
    NSString *encipherHeadrSreing = _encipherTextField.text;
    NSString *postString = @"http://tool.zzblo.com/api/md5/encrypt";
    NSString *paramenters = [NSString stringWithFormat:@"text=%@", encipherHeadrSreing];
    [self urlString:postString headString:encipherHeadrSreing paramenters:paramenters];
}
- (IBAction)decrypButton:(id)sender {
    NSString *decrypHeadString = _decrypTextField.text;
    NSString *postString = @"http://tool.zzblo.com/api/md5/decrypt";
    NSString *paramenters = [NSString stringWithFormat:@"secret=%@", decrypHeadString];
    [self urlString:postString headString:decrypHeadString paramenters:paramenters];
}
- (IBAction)MD5:(id)sender {
   NSString *string = [_decrypTextField.text stringWith32BitMD5Lower];
    NSLog(@"%@", string);
    _decrypTextField.text = string;
    NSLog(@"%@", _decrypTextField.text);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
